import Vue from "vue";
import Vuex from "vuex";
// import persistedState from "vuex-persistedstate";
Vue.use(Vuex);

const store = new Vuex.Store({
  // plugins: [persistedState({ storage: window.sessionStorage })],
  state: {
    users: {},
    token: "",
    tagsList: [],
    collapse: false, // 默认打开
  },
  mutations: {
    users(state, user) {
      state.users = user;
      window.sessionStorage.setItem("usersObj", JSON.stringify(user));

      state.token = user.token;
      window.sessionStorage.setItem("token", JSON.stringify(user.token));
    },
    setUserStatus(state, bool) {
      state.user_status = window.sessionStorage.setItem("status", bool);
    },
    setToken(state, token) {
      state.token = window.sessionStorage.setItem(
        "token",
        JSON.stringify(token)
      );
    },
    setUserInfo(state, user) {
      state.token = window.sessionStorage.setItem(
        "usersObj",
        JSON.stringify(user)
      );
    },

    delTagsItem(state, data) {
      state.tagsList.splice(data.index, 1);
    },
    setTagsItem(state, data) {
      state.tagsList.push(data);
    },
    //关闭所有选项卡
    clearTags(state) {
      state.tagsList = [];
    },
    // 关闭其他
    closeTagsOther(state, data) {
      state.tagsList = data;
    },
    // 关闭当前
    closeCurrentTag(state, data) {
      for (let i = 0, len = state.tagsList.length; i < len; i++) {
        const item = state.tagsList[i];
        if (item.path === data.$route.fullPath) {
          if (i < len - 1) {
            data.$router.push(state.tagsList[i + 1].path);
          } else if (i > 0) {
            data.$router.push(state.tagsList[i - 1].path);
          } else {
            data.$router.push("/");
          }
          state.tagsList.splice(i, 1);
          break;
        }
      }
    },
    // 侧边栏折叠
    hadndleCollapse(state, data) {
      state.collapse = data;
    },
  },
  getters: {
    getUserInfo(state) {
      state.users = JSON.parse(window.sessionStorage.getItem("usersObj"));

      return state.users;
    },
    getToken(state) {
      state.token = JSON.parse(window.sessionStorage.getItem("token"));
      return state.token;
    },
  },
  actions: {},
});
export default store;
